package LCR;

import java.util.Deque;
import java.util.LinkedList;

public class maxSlidingWindow {


    public int[] maxSlidingWindow(int[] nums, int k) {
        if(nums.length == 0 || k == 0) {
            return new int[0];
        }
        Deque<Integer> de = new LinkedList<>();

        int[] ret = new int[nums.length - k + 1];
        for(int j =0,i = 1 -k;j < nums.length;i++,j++) {
            if(i > 0 && de.peekFirst() == nums[i-1]) {
                de.removeFirst();
            }
            while(!de.isEmpty() && de.peekLast() < nums[j]) {
                de.removeLast();
            }
            de.addLast(nums[j]);
            if(i >= 0) {
                ret[i] = de.peekFirst();
            }
        }
        return ret;

    }
}
